
//
//  EditBankCardViewController.m
//  Luxury
//
//  Created by DCQ on 2018/5/21.
//  Copyright © 2018年 ZNXZ. All rights reserved.
//

#import "EditBankCardViewController.h"
#import "EditBankCardFooterView.h"
#import "EditBankCardCell.h"
#import "WalletBankListViewController.h"
#import "UserInfoModel.h"

NSString * const EditBankCardFooterViewIdentifier = @"EditBankCardFooterViewIdentifier";
NSString * const EditBankCardCellIdentifier = @"EditBankCardCellIdentifier";

@interface EditBankCardViewController ()

@property(nonatomic, copy) NSString *real_name;
@property(nonatomic, copy) NSString *account_name;
@property(nonatomic, assign) BankCardVCType  type;
@property(nonatomic, strong) NSDictionary * bankInfo;
@property(nonatomic, copy) NSString * user_bank_id;
@end

@implementation EditBankCardViewController
- (instancetype)initWithType:(BankCardVCType )type  withBankInfo:(NSDictionary *)bankInfo{
    self = [super init];
    if (self) {
        self.type =  type;
        self.bankInfo = bankInfo;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (self.type == BankCardVCType_add) {
        [self setNavigationItemTitle:@"添加银行卡"];
    }else if (self.type == BankCardVCType_edit){
        [self setNavigationItemTitle:@"编辑银行卡"];
    }
    if (self.bankInfo) {
        self.account_name = self.bankInfo[@"account_name"];
        self.real_name = self.bankInfo[@"real_name"];
        self.user_bank_id = self.bankInfo[@"user_bank_id"];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)registerCell{
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([EditBankCardCell class]) bundle:nil] forCellReuseIdentifier:EditBankCardCellIdentifier];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([EditBankCardFooterView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:EditBankCardFooterViewIdentifier];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rows = [[self getDefaultData] count];
    
    return rows;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = 60;
    
    return height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    EditBankCardCell * tempCell = [tableView dequeueReusableCellWithIdentifier:EditBankCardCellIdentifier];
    BOOL enabled = NO;
    NSArray * datas = [self getDefaultData];
    NSString * title = datas[indexPath.row][@"title"];
    NSString * placeholder = datas[indexPath.row][@"placeholder"];
    NSString * content = @"";
    if (indexPath.row == 0) {
        if (self.bankInfo) {
            content = self.bankInfo[@"bank_name"];
        }
        enabled = NO;
        
    }else if (indexPath.row == 1){
        enabled = YES;
        content = self.real_name;
         [tempCell setupTFKeyboardType:UIKeyboardTypeDefault];
    }else if (indexPath.row == 2){
        enabled = YES;
        content = self.account_name;
        [tempCell setupTFKeyboardType:UIKeyboardTypePhonePad];
    }
    [tempCell setupTitle:title withPlaceholder:placeholder withContent:content];
    tempCell.indexPath = indexPath;
    tempCell.inputBlock = ^(NSIndexPath *indexPath, NSString *inputText) {
        if (indexPath.row == 1) {
            self.real_name = inputText;
        }else if (indexPath.row == 2){
            self.account_name = inputText;
        }
    };
    [tempCell setupTFEnabled:enabled];
    cell = tempCell;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (NSArray *)getDefaultData{
    NSArray * tempArray = @[@{@"title":@"银行名称",@"placeholder":@"选择开卡银行"},
                            @{@"title":@"开户名",@"placeholder":@"填写开户名"},
                            @{@"title":@"银行卡号",@"placeholder":@"填写银行卡号"}];
    return tempArray;
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footerView = nil;
    EditBankCardFooterView * tempView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:EditBankCardFooterViewIdentifier];
    tempView.btnBlock = ^{
        [self changeOrAddBank];
    };
    footerView = tempView;
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [self gotoChooseBankName];
    }
}
- (void)gotoChooseBankName{
    WalletBankListViewController * bankListVC= [[WalletBankListViewController alloc]init];
    bankListVC.selectedBlock = ^(NSDictionary *bankInfo) {
        self.bankInfo = bankInfo;
        [self.tableView reloadData];
    };
    [self pushViewController:bankListVC];
}

- (void)changeOrAddBank{
    
    if (!self.bankInfo || !self.account_name || !self.real_name) {
        [self showAlertView:@"请完善信息" withConfirmAction:nil];
        return ;
    }
    //    token    是    string    用户token
    //    bank_id    是    int    开卡银行ID
    //    account_name    是    string    银行卡号
    //    real_name    是    string    开户名
    //    user_bank_id    否    int    用户银行卡ID（修改时必填）
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    [params addEntriesFromDictionary:@{@"token":UserInfo.userToken}];
    if (self.bankInfo) {
        [params addEntriesFromDictionary:@{@"bank_id":self.bankInfo[@"bank_id"]}];
    }
    
    [params addEntriesFromDictionary:@{@"account_name":self.account_name}];
    [params addEntriesFromDictionary:@{@"real_name":self.real_name}];
    if (self.type == BankCardVCType_edit) {
        [params addEntriesFromDictionary:@{@"user_bank_id":self.user_bank_id}];
    }
    
    
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Wallet/addOrEditBank" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        if (!error) {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"UPATE_BANKINFO_NOTIFI" object:nil];
            [strongSelf backViewController];
        }
        
    }];
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

